package com.crm.controller.enqController;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.crm.dao.OpportunityDAO;
import com.crm.dao.PartsDAO;
import com.crm.entity.Business;
import com.crm.entity.Opportunity;
import com.crm.entity.Parts;


@Controller
@RequestMapping("/editEnq")
public class EditEnqController {
	protected final Log logger = LogFactory.getLog(getClass());
	//OpportunityDAO opportunityDAO=OpportunityDAO().gets;
	
	@RequestMapping(value="/edit.do",method = RequestMethod.GET)
	public ModelAndView getSrchEnqTable(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		String month=req.getParameter("mnth");
	String year=req.getParameter("year");
	logger.info("month is "+month);
	logger.info("year is "+year);
		
		List<Opportunity> enqs=null;
		try {
		
			enqs=OpportunityDAO.getSingleInstance().getenqs(month,year,bus.getBusinessId());
				
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		model.setViewName("/jsp/srchEnqTable.jsp");
		model.addObject("srchEnqList",enqs);
		return model; 
	}
	
	@RequestMapping(value="/setSrchEnq.do",method = RequestMethod.GET)
	public ModelAndView getSrchEnq(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		int busid=bus.getBusinessId();
		String refno=req.getParameter("refNo");
		logger.info("ref no "+refno);
		
		Opportunity op=OpportunityDAO.getSingleInstance().getByRefNo(busid,refno);
		int id=op.getId();
		logger.info("id is "+id);
		PartsDAO prtdao=new PartsDAO();
		List<Parts> prt= prtdao.getPartsByIds(busid,id);
		model.setViewName("/jsp/editEnquiry.jsp");
		model.addObject("enqDet",op);
		model.addObject("partslst",prt);
		model.addObject("compList",bus);
		model.addObject("submenu","enquiry");
		model.addObject("menuName","enquiry");
		return model;
		
	}
}
